﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

namespace JV.Com.Tools
{
    public static class SerializerHelper
    {
        public static string Serialize<T>(T value)
        {
            var builder = new StringBuilder();
            using (var xmlWriter = XmlWriter.Create(builder))
            {
                 var serializer = new DataContractSerializer(typeof(T));
                serializer.WriteObject(xmlWriter, value);
            }
            return builder.ToString();
        }

        public static T Deserialize<T>(string data)
        {
            using(var reader = new StringReader(data))
            {
                using (var xmlReader = XmlReader.Create(reader))
                {
                    var serializer = new DataContractSerializer(typeof(T));
                    return (T)serializer.ReadObject(xmlReader);
                }
            }
        }
    }
}
